package com.bite.springredis.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Set;

@Slf4j
@RestController
@RequestMapping("/test")
public class RedisController {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @RequestMapping("/testString")
    public String testString(){
        log.info("testString");
        redisTemplate.opsForValue().set("key1","value1");
        redisTemplate.opsForValue().set("key2","value2");
        redisTemplate.opsForValue().set("key3","value3");

        String value = redisTemplate.opsForValue().get("key1");
        System.out.println("value: "+value);
        return "value: "+value;
    }

    @RequestMapping("/testList")
    public String testList(){

        redisTemplate.execute((RedisConnection connection) -> {
            connection.flushAll();
            return null;
        });

        redisTemplate.opsForList().leftPush("key1","111");
        redisTemplate.opsForList().leftPush("key1","222");
        redisTemplate.opsForList().leftPush("key1","333");

        String value = redisTemplate.opsForList().rightPop("key1");
        System.out.println("value: "+value);
        value = redisTemplate.opsForList().rightPop("key1");
        System.out.println("value: "+value);
        value = redisTemplate.opsForList().rightPop("key1");
        System.out.println("value: "+value);

        return "ok";
    }

    @RequestMapping("/testSet")
    public String testSet(){
        redisTemplate.execute((RedisConnection connection) -> {
            connection.flushAll();
            return null;
        });

        redisTemplate.opsForSet().add("key","111","222","333");
        Set<String> members = redisTemplate.opsForSet().members("key");
        System.out.println("members: "+members);

        Boolean result = redisTemplate.opsForSet().isMember("key", "333");
        System.out.println("isMember: "+result);

        Long size = redisTemplate.opsForSet().size("key");
        System.out.println("size: "+size);
        return "OK";

    }


    @RequestMapping("/testHash")
    public String testHash(){
        redisTemplate.execute((RedisConnection connection) -> {
            connection.flushAll();
            return null;
        });
        redisTemplate.opsForHash().put("key","f1","111");
        HashMap<String,String> map = new HashMap<>();
        map.put("f2","222");
        map.put("f3","333");
        redisTemplate.opsForHash().putAll("key",map);

        String value = (String)redisTemplate.opsForHash().get("key", "f1");
        System.out.println("value: "+value);

        Boolean hasKey = redisTemplate.opsForHash().hasKey("key", "f1");
        System.out.println("hasKey: "+hasKey);

        Long delete = redisTemplate.opsForHash().delete("key", "f1","f2");
        System.out.println("delete: "+delete);

        Long size = redisTemplate.opsForHash().size("key");
        System.out.println("size: "+size);

        return "OK";
    }

    @RequestMapping("/testZset")
    public String testZset(){
        redisTemplate.execute((RedisConnection connection) -> {
            connection.flushAll();
            return null;
        });
        redisTemplate.opsForZSet().add("key","张三",93);
        redisTemplate.opsForZSet().add("key","李四",94);
        redisTemplate.opsForZSet().add("key","王五",95);

        Set<String> range = redisTemplate.opsForZSet().range("key", 0, -1);
        System.out.println("range: "+range);

        Set<ZSetOperations.TypedTuple<String>> rangeWithScores = redisTemplate.opsForZSet().rangeWithScores("key", 0, 1);
        System.out.println("rangeWithScores: "+rangeWithScores);

        Double scoreZhangsan = redisTemplate.opsForZSet().score("key", "张三");
        System.out.println("score: "+scoreZhangsan);

        Long remove = redisTemplate.opsForZSet().remove("key", "张三");
        System.out.println("remove: "+remove);

        Long size = redisTemplate.opsForZSet().size("key");
        System.out.println("size: "+size);

        Long rank = redisTemplate.opsForZSet().rank("key", "李四");
        System.out.println("rank: "+rank);

        return "OK";


    }


}
